
angular.module('aaser.controllers').controller('EntityMappingController', ['$scope', '$filter', 'Resources', 'Assignment', 'Authorization',
    function ($scope, $filter, AaserResources, Assignment, Authorization) {

      //  Authorization({'classification-type': 'global', 'classification-name': 'GLOBAL', 'permission': 'CONFIGURE_AUTH'});

        $scope.roleAssignments = AaserResources.roles.query();
        $scope.entityType = 'user';
        $scope.entities = '';
        $scope.selectedEntities = [];
        $scope.selectedAssignment = '';

        confirmationClose();

        $scope.$watch('entityType', function () {
            switch ($scope.entityType) {
                case 'user':
                    $scope.entities = AaserResources.users.query();
                    $scope.selectedEntities = [];
                    $scope.titleOfTable = "Users";
                    break;
                case 'group':
                    $scope.entities = AaserResources.groups.query();
                    $scope.selectedEntities = [];
                    $scope.titleOfTable = "Groups";
                    break;
            }
        });



        $scope.selectEntity = function (entity) {
            if ($scope.isEntitySelected(entity)) {
                var index = $scope.selectedEntities.indexOf(entity);
                $scope.selectedEntities.splice(index, 1);
            } else {
                $scope.selectedEntities.push(entity);
            }

        };
        $scope.selectAssignment = function (assignment) {
            if (assignment == $scope.selectedAssignment) {
                $scope.selectedAssignment = '';
            } else {
                $scope.selectedAssignment = assignment;
            }
        };


        $scope.isAssignmentSelected = function (assignment) {
            return assignment == $scope.selectedAssignment;
        };
        $scope.isEntitySelected = function (entity) {
            return $filter('filter')($scope.selectedEntities, {name: entity.name}, true).length > 0;
        };



        $scope.confirmAssignment = function () {
//  TODO: directive confirmationPopup should be in isolated scope
            console.log("Exec confirmation Assignment");
            if ($scope.selectedEntities.length > 0 && $scope.selectedAssignment != '') {
                $scope.confirmationDO = {
                    title: "Assignment confirmation",
                    message: "Please confirm assignment of role " + $scope.selectedAssignment.name + " to " + $scope.selectedEntitiesToString() +
                        "With classification: " + $scope.selectedClassificationToString(),
                    callbackOK: $scope.confirmed,
                    callbackCancel: $scope.canceled
                };
                confirmationOpen();
            }
        };

        /*
        *After confirmation this method create an assignment object and send it via rest
         */
        $scope.confirmed = function () {

                angular.forEach($scope.selectedEntities, function (entity) {
                    var assignOfEnt = {};
                    assignOfEnt[$scope.entityType] = entity.name;
                    Assignment.query(assignOfEnt).$promise.then(function (data) {
                        var newAssignment = getAssignmentObject(entity, classification, $scope.selectedAssignment, data);
                        console.log(newAssignment);
                        if (newAssignment)
                            newAssignment.$save();
                    });


                });


            confirmationClose();
        };

        $scope.canceled = function () {
             confirmationClose();
        };

        function confirmationOpen() {
           $scope.confirmPopup = true;
        }

        function confirmationClose() {

            $scope.confirmPopup = false;
            $scope.confirmationDo = '';

        }

        function getAssignmentObject(entity, classification, role, assignments) {
            var canBeAssigned = true;
            angular.forEach(assignments, function (assignment) {
                if (assignment.classification.id == classification.id && assignment.role.id == role.id) {
                    canBeAssigned = false;
                }
            });
            if (canBeAssigned) {
                return new Assignment({

                    ldapEntity: {
                        type: $scope.entityType.toUpperCase(),
                        name: entity.name
                    },
                    role: {
                        id: role.id
                    },
                    classification: classification
                });
            }
            else {
                return false
            }

        }

    }]);